package com.catazy.mvvmlib.ext

import okhttp3.MediaType
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.io.StringReader
import java.io.StringWriter
import java.util.Locale
import javax.xml.transform.OutputKeys
import javax.xml.transform.Source
import javax.xml.transform.TransformerFactory
import javax.xml.transform.stream.StreamResult
import javax.xml.transform.stream.StreamSource

/**
 * 作者: Tao
 * 日期: 2024/1/29
 * 时间: 11:20
 * 描述: Okhttp 扩展类
 */

/**
 * 判断 MediaType 是否是可解析的
 */
fun MediaType?.isParseable(): Boolean {
    return if (this == null) {
        false
    } else this.isText() || this.isPlain()
            || this.isJson() || this.isForm()
            || this.isHtml() || this.isXml()
}

/**
 * 判断 MediaType 是否是文本类型
 */
fun MediaType?.isText(): Boolean {
    if (this == null) return false
    return "text" == type()
}

/**
 * 判断 MediaType 是否是纯文本类型
 */
fun MediaType?.isPlain(): Boolean {
    if (this == null) return false
    return this.subtype().lowercase(Locale.getDefault()).contains("plain")
}

/**
 * 判断 MediaType 是否是 Json 类型
 */
fun MediaType?.isJson(): Boolean {
    if (this == null) return false
    return this.subtype().lowercase(Locale.getDefault()).contains("json")
}

/**
 * 判断 MediaType 是否是 Xml 类型
 */
fun MediaType?.isXml(): Boolean {
    if (this == null) return false
    return this.subtype().lowercase(Locale.getDefault()).contains("xml")
}

/**
 * 判断 MediaType 是否是 Html 类型
 */
fun MediaType?.isHtml(): Boolean {
    if (this == null) return false
    return this.subtype().lowercase(Locale.getDefault()).contains("html")
}

/**
 * 判断 MediaType 是否是 Form 类型
 */
fun MediaType?.isForm(): Boolean {
    if (this == null) return false
    return this.subtype().lowercase(Locale.getDefault()).contains("x-www-form-urlencoded")
}

/**
 * 字符串 Json格式化
 */
fun String?.jsonFormat(): String {
    if (this.isNullOrEmpty()) return "Empty/Null json content"
    var message: String
    var json = this
    try {
        json = json.trim { it <= ' ' }
        message = if (json.startsWith("{")) {
            val jsonObject = JSONObject(json)
            jsonObject.toString(4)
        } else if (json.startsWith("[")) {
            val jsonArray = JSONArray(json)
            jsonArray.toString(4)
        } else {
            json
        }
    } catch (e: JSONException) {
        message = json
    } catch (error: OutOfMemoryError) {
        message = "Output omitted because of Object size"
    }
    return message
}

/**
 * 字符串 Xml格式化
 */

fun String?.xmlFormat(): String {
    if (this.isNullOrEmpty()) return "Empty/Null xml content"
    val message: String = try {
        val xmlInput: Source =
            StreamSource(StringReader(this))
        val xmlOutput =
            StreamResult(StringWriter())
        val transformer =
            TransformerFactory.newInstance().newTransformer()
        transformer.setOutputProperty(OutputKeys.INDENT, "yes")
        transformer.setOutputProperty(
            "{http://xml.apache.org/xslt}indent-amount",
            "2"
        )
        transformer.transform(xmlInput, xmlOutput)
        xmlOutput.writer.toString().replaceFirst(">".toRegex(), ">\n")
    } catch (e: Exception) {
        this
    } catch (error: OutOfMemoryError) {
        "Output omitted because of Object size"
    }
    return message
}